from odoo import fields, models, api


class StockMove(models.Model):
    _inherit = 'stock.move'

    # product_uom_qty = fields.Float(string='Demand', inverse='_inverse_product_uom_qty', store=True, readonly=False)
    panel_need_qty = fields.Float(string='PANEL Need', compute='_compute_panel_need_qty', digits=(10, 3))
    panel_qty = fields.Float(string='PANEL NUM', compute='_compute_panel_need_qty', digits=(10, 3))
    # pcs_panel_ratio = fields.Float(string='pcs panel 换算比例', compute='_compute_panel_pcs_ratio', store=True, readonly=True)
    is_panel_visible = fields.Boolean(string='Panel is Show', compute='_compute_is_panel_visible')

    product_before_rounding_qty = fields.Float(string='product before rounding qty', copy=False)

    def _compute_is_panel_visible(self):
        for rec in self:
            rec.is_panel_visible = rec.panel_need_qty

    # @api.onchange('product_uom_qty')
    def onchange_s_product_uom_qty(self):
        if all([self.production_id, self.raw_material_production_id]):
            self.panel_need_qty = 0
            self.panel_qty = 0
        else:
            bom = self.production_id.bom_id or self.raw_material_production_id.bom_id

            self.panel_need_qty = self.product_uom_qty * bom.pcs_panel_ratio

    # @api.depends('product_id', 'product_uom', 'product_uom_qty', 'state')
    def _compute_panel_need_qty(self):
        for rec in self:
            if all([rec.production_id, rec.raw_material_production_id]):
                rec.panel_need_qty = 0
                rec.panel_qty = 0
                continue

            rec.panel_need_qty = rec.product_uom_qty / (rec.raw_material_production_id.s_value_pnl or 1)
            rec.panel_qty = rec.quantity / (rec.raw_material_production_id.s_value_pnl or 1)

    # def _subei_data_process(self):
    #     move_ids = self.search([('state', '=', 'done')], order='write_date')
    #     for move in move_ids:
    #         move._action_done()